package com.cy.ppj.comm.config;

import com.cy.ppj.comm.third.xy.ClientHttpRequestFactoryCreator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import java.nio.charset.StandardCharsets;

/**
 * @description: 小赢配置
 * @author: Ocean
 * @create: 2018-08-09 10:01
 **/
@Slf4j
@Configuration
public class XyConfig {

    @Value("${admission.xy.cert-file-path}")
    private String certFilePath;
    @Value("${admission.xy.key-file-path}")
    private String keyFilePath;


    @Bean
    public RestTemplate xyRestTemplate() {
        RestTemplate restTemplate = null;
        try {
            Resource certResource = new ClassPathResource(certFilePath);
            Resource keyResource = new ClassPathResource(keyFilePath);
            ClientHttpRequestFactoryCreator creator = new ClientHttpRequestFactoryCreator(certResource.getFile().getPath(), keyResource.getFile().getPath());
            restTemplate = new RestTemplate(creator.httpClientRequestFactory());
            restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        } catch (Exception e) {
            log.error("xy sslRestTemplate error:", e);
        }
        return restTemplate;
    }
}
